package com.fairytask.engine.components.checkBox
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.managers.ResManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class CheckBox extends BaseComponent
	{
		private var _bgSpace			:Sprite;
		private var _ChildSpace		:Sprite;
		private var _unselectedSkin	:MovieClip;
		private var _selectedSkin		:MovieClip;
		private var _currentSkin		:MovieClip;
		private var _isEvent			:Boolean=	true;
		private var _selected      	:Boolean;
		private var _enable			:Boolean=true;
		public static const CHECK_BOX_CLICK : String = "check_box_click";
		public function CheckBox(_Skin:String)
		{
			super();
			_bgSpace			=	new Sprite();
			_ChildSpace		=	new Sprite();
			addChild(_bgSpace);
			addChild(_ChildSpace);
			_bgSpace.buttonMode=true;
			skinStr(_Skin);
			initEvent();
		
		}
		private function initEvent():void
		{
			_isEvent	=	true;
			_bgSpace.addEventListener(MouseEvent.CLICK,onClick);
		}
		private function onClick(e:MouseEvent):void
		{
			if(!_enable)
			{
				return;
			}
			if(!_isEvent)
			{
				return;
			}
			selected	=	_selected?false:true;
			dispatchEvent(new Event(CHECK_BOX_CLICK));
		}
		private function skinStr(value:String):void
		{
			_skin = value;
			if(_skin=="")
			{
				drawBG();
			}else{
				var res:MovieClip	=	ResManager.newResClass(_skin) as MovieClip;
				_unselectedSkin		=	res.getChildByName("CheckBox_unselected") as MovieClip;
				_selectedSkin			=	res.getChildByName("CheckBox_selected") as MovieClip;
				//				resizeSkin();
				_unselectedSkin.x		=	_selectedSkin.x    =	0;
				_unselectedSkin.y		=	_selectedSkin.y    =	0;
				_bgSpace.addChild(_unselectedSkin);
				_currentSkin	=	_unselectedSkin;
			}
		}
		private function drawBG():void
		{
			_bgSpace.graphics.clear();
			_bgSpace.graphics.beginFill(0xff0000,0);
			_bgSpace.graphics.drawRect(0,0,0,0);
			_bgSpace.graphics.endFill();
		}
		private function clearBG():void
		{
			while(_bgSpace.numChildren>0)
			{
				_bgSpace.removeChildAt(0);
			}
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			
			clearBG();
			//不可用不执行
			if(_selected)
			{
				_bgSpace.addChild(_selectedSkin);
				_currentSkin	=	_selectedSkin;	
			}
			else
			{
				_bgSpace.addChild(_unselectedSkin);
				_currentSkin	=	_unselectedSkin;
			}
		}

		
		/**
		 *	是否可用 
		 * @return 
		 * 
		 */		
		public function get enable():Boolean
		{
			return _enable;
		}

		public function set enable(value:Boolean):void
		{
			_enable = value;
			if(_enable)
			{
				_unselectedSkin.filters = null;
				_selectedSkin.filters = null;
				_currentSkin.filters=null;
			}
			else
			{
				_unselectedSkin.filters = [Filter.GRAY];
				_selectedSkin.filters = [Filter.GRAY];
			}
		}
		
		/**
		 * 是否可以点选切换 
		 * @return 
		 * 
		 */		
		public function get isEvent():Boolean
		{
			return _isEvent;
		}
		
		public function set isEvent(value:Boolean):void
		{
			_isEvent = value;
			_isEvent ? _bgSpace.buttonMode=true : _bgSpace.buttonMode=false;
		}
	}
}